<template>
  <div class="adddep">
    <el-dialog :title="readtitle" :visible="digshow" @close="addbtn">
      <el-form :model="form" :rules="rules" ref="myfrom">
        <el-form-item
          label="部门名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="部门编码"
          :label-width="formLabelWidth"
          prop="code"
        >
          <el-input v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="部门负责人"
          :label-width="formLabelWidth"
          prop="manager"
        >
          <el-select v-model="form.manager" placeholder="请选择">
            <el-option
              v-for="(item, index) in peoplelist"
              :label="item.username"
              :key="index"
              :value="item.username"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="部门介绍"
          :label-width="formLabelWidth"
          prop="introduce"
        >
          <el-input v-model="form.introduce" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addbtn">取 消</el-button>
        <el-button type="primary" @click="subbit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getpeople,
  geteverylist,
  addcompany,
  eaitcompany,
} from "../../api/departments";
export default {
  props: ["digshow", "currentItem", "type"],
  data() {
    return {
      peoplelist: 0,
      getevery: 0,
      form: {
        name: "",
        code: "",
        introduce: "",
        manager: "",
      },
      formLabelWidth: "120px",
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          { min: 1, max: 50, message: "1-50个字符", trigger: "blur" },
          {
            validator: async (rule, value, callback) => {
              let res = await geteverylist();
              if (this.type == "add") {
                let bool = res.depts
                  .filter((item) => {
                    return item.pid === this.currentItem.id;
                  })
                  .some((item) => {
                    return value === item.name;
                  });
                bool ? callback(new Error("部门名称重复")) : callback();
              } else {
                let bool2 = res.depts
                  .filter((item) => {
                    return item.pid === this.currentItem.id;
                  })
                  .some((item) => {
                    return (
                      value === item.name && item.id !== this.currentItem.id
                    );
                  });
                bool2 ? callback(new Error("部门名称重复")) : callback();
              }
            },
          },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          { min: 1, max: 50, message: "1-50个字符", trigger: "blur" },
          {
            validator: async (rule, value, callback) => {
              let res = await geteverylist();
              if (this.type == "add") {
                let boll = res.depts.some((item) => {
                  return value == item.code;
                });
                boll ? callback(new Error("部门编码重复")) : callback();
              } else {
                let boll2 = res.depts.some((item) => {
                  return value == item.code && item.id !== this.currentItem.id;
                });
                boll2 ? callback(new Error("部门编码重复")) : callback();
              }
            },
          },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          { min: 1, max: 300, message: "1-300个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    addbtn() {
      this.$emit("closedig", false);
      this.form = {
        name: "",
        code: "",
        introduce: "",
        manager: "",
      };
      this.$refs.myfrom.resetFields();
    },
    subbit() {
      this.$refs.myfrom.validate(async (isok) => {
        if (isok) {
          if (this.type === "add") {
            try {
              await addcompany({
                ...this.form,
                pid: this.currentItem.id,
              });
              this.$message({
                type: "success",
                message: "新增成功",
              });
              this.$emit("closedig", false);
              this.form = {
                name: "",
                code: "",
                introduce: "",
                manager: "",
              };
              this.$emit("updatedata");
            } catch {
              console.log("新增出错");
            }
          } else {
            try {
              await eaitcompany(this.form);
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.$emit("closedig", false);
              this.form = {
                name: "",
                code: "",
                introduce: "",
                manager: "",
              };
              this.$emit("updatedata");
            } catch {
              console.log("修改失败");
            }
          }
        }
      });
    },
  },
  async created() {
    let res = await getpeople();
    this.peoplelist = res;
    let res1 = await geteverylist();
    this.getevery = res1;
  },
  computed: {
    readtitle() {
      return this.type === "add" ? "新增部门" : "修改部门";
    },
  },
};
</script>

<style lang='scss'>
</style>